// pages/allMeetingRoom/allMeetingRoom.js
import utils from "./../../utils/utils"
var data = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNull: false,
    select: false,
    titleBar: '全部',
    allRecordsList: [],
    openId: null
  },

  /* 点击头部切换筛选条件 */
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },

  /* 点击下拉列表元素 */
  mySelect(e) {

    wx.showLoading({
      title: '载入数据中',
    })
    var barFlag = e.currentTarget.dataset.barflag;

    this.setData({
      select: !this.data.select
    })

    if (barFlag === "0") { //全部
      //之前的数据全部清空
      this.setData({
        titleBar: "全部",
        allRecordsList: [],
        isNull: false
      })

      wx.cloud.database().collection("appointment").where({
        meetingPublishAdm: this.data.openId
      }).get({
        success: res => {
          wx.hideLoading();
          let data = res.data;
          if (data.length > 0) {
            this.setData({
              allRecordsList: data
            })
          } else {
            this.setData({
              isNull: true
            })
          }
        }
      })

      console.log("点击了全部：", this.data.allRecordsList)

    } else if (barFlag === "1") { //待审核
      //之前的数据全部清空
      this.setData({
        titleBar: "待审核",
        allRecordsList: [],
        isNull: false
      })

      wx.cloud.database().collection("appointment").where({
        isDone: 1,
        meetingPublishAdm: this.data.openId
      }).get().then(res => {
        wx.hideLoading();
        let data = res.data;
        if (data.length > 0) {
          this.setData({
            allRecordsList: data
          })
        } else {
          this.setData({
            isNull: true
          })
        }
      })


    } else if (barFlag === "2") { //已通过
      //之前的数据全部清空
      this.setData({
        titleBar: "已通过",
        allRecordsList: [],
        isNull: false
      })

      wx.cloud.database().collection("appointment").where({
        isDone: 2,
        meetingPublishAdm: this.data.openId
      }).get().then(res => {
        wx.hideLoading();
        let data = res.data;
        if (data.length > 0) {
          this.setData({
            allRecordsList: data
          })
        } else {
          this.setData({
            isNull: true
          })
        }
      })


    } else if (barFlag === "3") { //已驳回
      //之前的数据全部清空
      this.setData({
        titleBar: "已驳回",
        allRecordsList: [],
        isNull: false
      })

      wx.cloud.database().collection("appointment").where({
        isDone: 3,
        meetingPublishAdm: this.data.openId
      }).get().then(res => {
        wx.hideLoading();
        let data = res.data;
        if (data.length > 0) {
          this.setData({
            allRecordsList: data
          })
        } else {
          this.setData({
            isNull: true
          })
        }
      })


    } else if (barFlag === "4") { //已过期
      //之前的数据全部清空
      this.setData({
        titleBar: "已过期",
        allRecordsList: [],
        isNull: false
      })

      wx.cloud.database().collection("appointment").where({
        isDone: 4,
        meetingPublishAdm: this.data.openId
      }).get().then(res => {
        wx.hideLoading();
        let data = res.data;
        if (data.length > 0) {
          this.setData({
            allRecordsList: data
          })
        } else {
          this.setData({
            isNull: true
          })
        }
      })

    }
  },

  /* 加载数据 */
  async getList() {
    console.log(this.data.isNull)
    wx.showLoading({
      title: '载入数据中',
    })
    let res = await wx.cloud.database().collection("appointment").where({
      meetingPublishAdm: this.data.openId
    }).skip(this.page * this.PAGE).limit(this.PAGE).get({})
    wx.hideLoading()
    let data = res.data;
    if (data.length > 0) {
      //对数据进行处理
      for (let i = 0; i < data.length; i++) {
        if (data[i].isDone === 1) {
          let flag = utils.setTime(data[i].checkMyDate, data[i].checkDay);
          console.log(flag)
          if (flag) { //已经过期，修改数据
            console.log(flag)
            data[i].isDone = 4;
            let appointId = data[i]._id;
            console.log(appointId);
            console.log(data[i])
            //数据库同步数据
            wx.cloud.database().collection("appointment").doc(appointId).update({
              data: {
                isDone: 4
              }
            })
          }
        }
      }

      wx.hideLoading();
      if (this.init) { //初始化
        this.setData({
          allRecordsList: data
        })
      } else { //追加
        let conData = this.data.allRecordsList.concat(data);
        this.setData({
          allRecordsList: conData
        })
      }
    } else {
      if (this.init) {
        wx.hideLoading();
        this.setData({
          isNull: true
        })
      }

    }

  },

  /* 触底时进行加载 */
  async onReachBottom() {
    this.page += 1;
    this.init = false;
    await this.getList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.page = 0;
    this.PAGE = 6;
    this.init = true;
    let openId = getApp().globalData.userInfo._openid
    this.setData({
      openId: openId,
      allRecordsList: [],
      titleBar: '全部',
      isNull: false
    })



    await this.getList()

    // //后台请求数据
    // wx.cloud.database().collection("appointment").where({
    //   meetingPublishAdm: openId
    // }).get({
    //   success: res => {
    //     wx.hideLoading();
    //     let data = res.data;
    //     if (data.length > 0) {
    //       //对数据进行处理
    //       for (let i = 0; i < data.length; i++) {
    //         if (data[i].isDone === 1) {
    //           let flag = utils.setTime(data[i].checkMyDate, data[i].checkDay);
    //           console.log(flag)
    //           if (flag) { //已经过期，修改数据
    //             console.log(flag)
    //             data[i].isDone = 4;
    //             let appointId = data[i]._id;
    //             console.log(appointId);
    //             console.log(data[i])
    //             //数据库同步数据
    //             wx.cloud.database().collection("appointment").doc(appointId).update({
    //               data: {
    //                 isDone: 4
    //               }
    //             })
    //           }
    //         }
    //       }

    //       wx.hideLoading();
    //       this.setData({
    //         allRecordsList: data
    //       })

    //     } else {
    //       wx.hideLoading();
    //       this.setData({
    //         isNull: true
    //       })
    //     }
    //   }
    // })
  },


  /* 展示记录详情 */
  detailPanel: function (options) {
    wx.navigateTo({
      url: '/pages/recordsDetial/recordsDetial?record=' + options.currentTarget.dataset.record + "&flag=" + options.currentTarget.dataset.flag
    })
  },


  async onShow() {
    this.onLoad()
  }
})